<template>
  <el-dialog
    :visible.sync="visible"
    :title="`${type == 'add' ? '新建' : '修改'}模板`"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      label-position="right"
      label-width="80px"
      style="width: 500px; margin-left: 50px"
    >
      <el-form-item label="模板名称" prop="strategyName">
        <el-input
          v-model="dataForm.formworkName"
          placeholder="请输入模板名称"
          style="width: 360px"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        :disabled="!dataForm.formworkName"
        @click="addSubmit()"
      >
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrUpdateFormworkAndCondition } from "@/api/policyManage";
export default {
  data() {
    return {
      type: "add",
      visible: false,
      dataForm: {
        searchStrategyTypeDTO: {}
      }
    };
  },
  props: {
    strategyPool: {
      type: Number
    }
  },
  methods: {
    init(type, params, userId) {
      this.visible = true;
      this.type = type;
      this.dataForm = {
        searchStrategyTypeDTO: {}
      };
      this.$nextTick(() => {
        this.dataForm.userId = userId;
        if (type == "add") {
          this.dataForm.searchStrategyTypeDTO = params;
        } else {
          this.dataForm = {
            searchStrategyTypeDTO: params.formworkConditionVo[0],
            formworkId: params.formworkId,
            formworkName: params.formworkName,
            mark: params.mark,
            userId: userId
          };
        }
      });
    },
    addSubmit() {
      addOrUpdateFormworkAndCondition({
        strategyPool: this.strategyPool,
        ...this.dataForm
      }).then((res) => {
        if (res.code == 0) {
          this.$notify({
            type: "success",
            message: `${this.type == "add" ? "新建" : "修改"}成功`,
            duration: 2000
          });
          this.visible = false;
          this.$emit("closeOpen");
        }
      });
    }
  }
};
</script>
